package com.sskj.fabi.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.bean.FabiBuyBean;
import com.sskj.fabi.bean.FabiListBean;
import com.sskj.fabi.bean.PayWayBean;
import com.sskj.fabi.http.HttpConfig;
import com.sskj.fabi.ui.fragment.MarketBuyAndSellFragment;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.List;

import io.reactivex.Flowable;


public class MarketBuyAndSellFragmentPresenter extends BasePresenter<MarketBuyAndSellFragment> {
    /**
     * 获取用户交易大厅数据
     */


    public Flowable<List<FabiListBean>> getFabiList(int page, int size, boolean isBuy) {
        return httpService.getFbList(page, size, isBuy)
                .converter(new JsonConverter<HttpData<PageBean<FabiListBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList());
    }

    /**
     * 获取用户支付方式
     */
    public void requsetPayWayList() {
        httpService.getPayList()
                .execute(new JsonCallBack<HttpData<List<PayWayBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<List<PayWayBean>>> response) {
                        mView.onPayWaySuccess(response.body().getData());
                    }

                    @Override
                    public void onError(Response<HttpData<List<PayWayBean>>> response) {
                        super.onError(response);
                    }
                });
    }

    public void order(String tpwd, String order_no, String total_num, String total_price, boolean isBuy, String payType) {
        httpService.coinOrder(tpwd, order_no, total_num, total_price, isBuy ? 1 : 2, payType)
                .execute(new JsonCallBack<HttpData<FabiBuyBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<FabiBuyBean>> response) {
                        if (response.body().getStatus() == HttpConfig.OK) {
                            mView.orderSuccess();
//                            mView.checkOk();
                        }
                        ToastUtil.showShort(response.body().getMsg());
                    }
                });
    }
}
